*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*** CODE TO REPRODUCE TABLES AND FIGURES IN MANUSCRIPT OF: 
*** "Obstacles on the road to school: 
*** The impacts of mobility restriction on educational performance" 
*** AUTHORS: Ines Lee, Sami Miaari
*** REVISED: 3 Sep 2021
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear 
set more off 
local date: display %td_CCYY_NN_DD date(c(current_date), "DMY")
global date_str = subinstr(trim("`date'"), " ", "_", .)

*** SET DIRECTORIES 
global root = "XXXX" // Update root folder
global rawdata = "$root/Data/RawData"
global cleandata = "$root/Data/CleanData"

*** GLOBALS FOR TABLES 
global starlevel = "star(* 0.10 ** 0.05 *** 0.01)"
global tabwidth "modelwidth(15) varwidth(30)"

*** LOG FILE 
cap log close
log using "$root/log_files/${date_str}_Main_results", replace
 
/*** -------------------------------------------------------------------------------
*** TABLE OF CONTENT ***
------------------------------------------------------------------------------- ***/
* Table 1: Descriptive statistics
* Table 2: Impact of checkpoints near school on educational performance 
* Table 3: Impact of checkpoints near school, by gender
* Table 4: Impact of encountering checkpoint on road to school 
* Table 5: Impact of checkpoint near school on learning environment 
* Figure 1: Map of physical barriers (provided by ARIJ)
* Figure 2: Evolution of conflict over time 
* Figure 3: Effect of additional checkpoints on educational performance 

/*** -------------------------------------------------------------------------------
*** PROGRAM TOGGLES ***
------------------------------------------------------------------------------- ***/
local make_csv_tables = 1
local tab_1 = 1
local tab_2 = 1
local tab_3 = 1
local tab_4 = 1
local tab_5 = 1
local fig_1 = 1
local fig_2 = 1
local fig_3 = 1

/*** ---------------------------------------------------------------------------
	 TABLE 1: DESCRIPTIVE STATISTICS 
--------------------------------------------------------------------------- ***/
if `tab_1' == 1 {

use "$cleandata/main_data.dta", clear
global desc_years = "2000 2002 2004 2006"
global mtitles = `" "All" "2000" "2002" "2004" "2006" "'

/* Create some variables */
gen art = branch == 1
gen science = branch == 2
gen vocational = branch >= 3 
gen muslim = religion == 1

bysort school_id year: gen unique_school_yr = _n==1
bysort destination year: gen unique_local_yr = _n==1
bysort school_id: gen unique_school = _n==1
bysort destination: gen unique_local = _n==1

g num_cp_10km = sch_num_cp_0_5km + sch_num_cp_5_10km
g num_alloth_10km = sch_num_alloth_0_5km + sch_num_alloth_5_10km

replace sch_fatalities = sch_fatalities*100 // sch fatalities are in 100s, so multiply by 100 to get count

capture est sto clear

/* Panel A: Obstacles */

qui estpost su num_cp_10km num_alloth_10km sch_fatalities if unique_school_yr == 1, listwise 

est sto allA

foreach yr in $desc_years {

qui estpost su num_cp_10km num_alloth_10km sch_fatalities if year==`yr' & unique_school_yr == 1, listwise 

est sto desc_`yr'A

}

esttab allA desc_2000A desc_2002A desc_2004A desc_2006A, cells(mean(fmt(2)) sd(par fmt(2))) title("Table 1: Panel A Conflict variables") coeflabels(num_cp_10km "# CP within 10km" num_alloth_10km "# other barriers within 10km" sch_fatalities "# Fatalities") mtitles($mtitles ) noobs eqlabels(none) collabels(none) 

/* Panel B: Student characteristics */

qui estpost su age female muslim art science vocational pass_exam average traverse , listwise 

est sto allB

foreach yr in $desc_years {

qui estpost su age female muslim art science vocational pass_exam average traverse if year==`yr' , listwise 

est sto desc_`yr'B
}

esttab allB desc_2000B desc_2002B desc_2004B desc_2006B, cells(mean(fmt(2)) sd(par fmt(2))) title("Table 1: Panel B Student variables") coeflabels(age "Age" female "Female" muslim "Muslim" art "Art" science "Science" vocational "Vocational" pass_exam "Pass" average "Average score" traverse "School in diff locality") mtitles($mtitles ) noobs eqlabels(none) collabels(none) 

/* Panel C: Classroom characteristics */
qui estpost su num_classrooms total_teachers total_students if unique_school_yr==1, listwise 

qui count
qui estadd scalar N_stu=r(N)
qui count if unique_school == 1
qui estadd scalar N_sch=r(N)
count if unique_local == 1
qui estadd scalar N_local=r(N)

est sto allC

foreach yr in $desc_years {

qui estpost su num_classrooms total_teachers total_students if year==`yr' & unique_school_yr==1, listwise 

qui count if year == `yr'
qui estadd scalar N_stu=r(N)
qui count if year == `yr' & unique_school_yr == 1
qui estadd scalar N_sch=r(N)
count if year == `yr' & unique_local_yr == 1
qui estadd scalar N_local=r(N)

est sto desc_`yr'C

}

esttab allC desc_2000C desc_2002C desc_2004C desc_2006C, cells(mean(fmt(2)) sd(par fmt(2))) title("Table 1: Panel C Classroom variables") coeflabels(num_classrooms "Number of classrooms" total_teachers "Total teachers" total_students "Total students") stats(N_stu N_sch N_local, label("# Students" "# Schools" "# Localities") fmt(%18.0f %18.0f %18.0f)) mtitles($mtitles ) noobs eqlabels(none) collabels(none)

if `make_csv_tables'==1 {

local tab_notes = "Notes: This table presents summary statistics for the main sample. Averages are taken across schools in panels A and C. Averages are taken across students in panel B. Standard deviation in parentheses."

esttab allA desc_2000A desc_2002A desc_2004A desc_2006A using "$root/Output/Table1.csv", replace cells(mean(fmt(2)) sd(par fmt(2))) title(`tab_caption') coeflabels(num_cp_10km "# CP within 10km" num_alloth_10km "# other barriers within 10km" sch_fatalities "# Fatalities") mtitles($mtitles ) refcat(num_cp_10km "A. Conflict variables", nolabel) noobs eqlabels(none) collabels(none) 

esttab allB desc_2000B desc_2002B desc_2004B desc_2006B using "$root/Output/Table1.csv", append cells(mean(fmt(2)) sd(par fmt(2))) coeflabels(age "Age" female "Female" muslim "Muslim" art "Art" science "Science" vocational "Vocational" pass_exam "Pass" average "Average score" traverse "School in diff locality") refcat(age "B. Student variables", nolabel) nomtitles nonum noobs eqlabels(none) collabels(none) 

esttab allC desc_2000C desc_2002C desc_2004C desc_2006C using "$root/Output/Table1.csv", append cells(mean(fmt(2)) sd(par fmt(2))) coeflabels(num_classrooms "Number of classrooms" total_teachers "Total teachers" total_students "Total students") refcat(num_classrooms "C. Classroom variables", nolabel) stats(N_stu N_sch N_local, label("Number of students" "Number of schools" "Number of localities") fmt(%18.0f %18.0f %18.0f)) nomtitles nonum noobs eqlabels(none) collabels(none) addnotes("`tab_notes'")

}
}

/*** ---------------------------------------------------------------------------
	 TABLE 2: 
--------------------------------------------------------------------------- ***/
if `tab_2' == 1 {

use "$cleandata/main_data.dta", clear

gen got_cp_0_10km = (sch_num_cp_0_5km>=1 | sch_num_cp_5_10km>=1)

global y_vars = "pass std_score std_math std_ara" // outcome variables
global stu_cntl = "female i.religion i.branch i.birth_year i.year" // student controls
global school_cntl = "num_classrooms total_students total_teachers" // school controls
global sch_oth_conflict = "sch_fatalities" // school other conflict controls
global sch_loc_cntl = "sch_pop10km" // school locality controls 
global FE = "school_id" // fixed effects 
global cluster_var = "destination" // cluster variable 

foreach y in $y_vars {

if "`y'" == "pass" local y_untransformed = "pass_exam"
if "`y'" == "std_score" local y_untransformed = "average"
if "`y'" == "std_math" local y_untransformed = "math"
if "`y'" == "std_ara" local y_untransformed = "ara"

qui areg `y'  got_cp_0_10km ${sch_oth_conflict} ${stu_cntl} ${school_cntl} ${sch_loc_cntl}, absorb(${FE}) vce(cluster ${cluster_var}) 

qui su `y_untransformed'
qui estadd scalar y_mu=r(mean)
qui estadd scalar y_sd=r(sd)

qui estadd local stu_cntl "Y" 
qui estadd local sch_cntl "Y" 
qui estadd local sch_FE "Y" 
qui estadd local year_FE "Y" 
est store `y'


}

global keepvar = "got_cp_0_10km sch_fatalities"
global varlabels = `" got_cp_0_10km ">= 1 CP within 10 km" sch_fatalities "Fatalities (100s)" "'  

global stats = `" y_mu y_sd stu_cntl sch_cntl sch_FE year_FE k_absorb N_clust N ,fmt(%6.2f %6.2f %~#s %~#s %~#s %~#s %18.0f %18.0f %18.0f) labels("Dep var mean" "Dep var SD" "Student controls" "School controls" "School FE" "Year FE" "Number of schools" "Number of school localities" "Observations") "'

local tab_caption = "Table 2: Impact of introduction of checkpoints (CPs) near school"
local tab_notes = "Notes: This table presents estimated coefficients from equation (1) where the obstacle of interest are checkpoints. Scores expressed in standard deviations normalized by study stream and year. All regressions include the following controls: population size of Israeli settlements within 10 km of school locality (in 100s), student controls (gender, religion, year of birth, study branch) and school controls (number of classrooms, number of students, number of teachers). All regressions include school and academic year fixed effects. Standard errors in parentheses, clustered at the school locality level.* p<0.10 ** p<0.05 *** p<0.01."

esttab pass std_score std_math std_ara, noomitted keep(${keepvar}) coeflabels(${varlabels}) mtitles("Pass" "Overall score" "Maths" "Arabic") b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(got_cp_0_10km "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes gaps $starlevel $tabwidth

if `make_csv_tables'==1 {

esttab pass std_score std_math std_ara using "$root/Output/Table2.csv", replace keep(${keepvar}) coeflabels(${varlabels}) mtitles("Pass" "Overall score" "Maths" "Arabic") b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(got_cp_0_10km "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes gaps $starlevel
}

}

/*** ---------------------------------------------------------------------------
	 TABLE 3: HETEROGENEITY BY GENDER 
--------------------------------------------------------------------------- ***/

if `tab_3' == 1 {

use "$cleandata/main_data.dta", clear

gen got_cp_0_10km = (sch_num_cp_0_5km>=1 | sch_num_cp_5_10km>=1)

cap drop male
g male = female==0 if !mi(female)

cap drop *_male
gen got_cp_0_10km_male= got_cp_0_10km*male
gen sch_fatalities_male= sch_fatalities*male
gen sch_pop10km_male= sch_pop10km*male

global y_vars = "pass std_score std_math std_ara" // outcome variables
global stu_cntl = "female i.religion i.branch i.birth_year i.year" // student controls
global school_cntl = "num_classrooms total_students total_teachers" // school controls
global sch_oth_conflict =  "sch_fatalities sch_fatalities_male" // school other conflict controls
global sch_loc_cntl = "sch_pop10km" // school locality controls 
global FE = "school_id" // fixed effects 
global cluster_var = "destination" // cluster variable 

foreach y in $y_vars {

if "`y'" == "pass" local y_untransformed = "pass_exam"
if "`y'" == "std_score" local y_untransformed = "average"
if "`y'" == "std_math" local y_untransformed = "math"
if "`y'" == "std_ara" local y_untransformed = "ara"

qui areg `y'  got_cp_0_10km got_cp_0_10km_male ${sch_oth_conflict} ${stu_cntl} ${school_cntl} ${sch_loc_cntl}, absorb(${FE}) vce(cluster ${cluster_var}) 

qui su `y_untransformed' if male==1
qui estadd scalar y_mu_male=r(mean)
qui estadd scalar y_sd_male=r(sd)

qui su `y_untransformed' if male==0
qui estadd scalar y_mu_female=r(mean)
qui estadd scalar y_sd_female=r(sd)

qui estadd local stu_cntl "Y" 
qui estadd local sch_cntl "Y" 
qui estadd local sch_FE "Y" 
qui estadd local year_FE "Y" 
est store `y'

}

global mtitles = `" "Pass" "Overall score" "Maths" "Arabic" "'

global keepvar = "got_cp_0_10km got_cp_0_10km_male sch_fatalities sch_fatalities_male"
global varlabels = `" got_cp_0_10km ">= 1 CP within 10 km" got_cp_0_10km_male ">= 1 CP within 10km X Male" sch_fatalities "Fatalities (100s)" sch_fatalities_male "Fatalities (100s) X Male" "'  

global stats = `" y_mu_male y_sd_male y_mu_female y_sd_female stu_cntl sch_cntl sch_FE year_FE k_absorb N_clust N ,fmt(%6.2f %6.2f %6.2f %6.2f %~#s %~#s %~#s %~#s %18.0f %18.0f %18.0f) labels("Dep var mean (male)" "Dep var SD (male)" "Dep var mean (female)" "Dep var SD (female)" "Student controls" "School controls" "School FE" "Year FE" "Number of schools" "Number of school localities" "Observations") "'

local tab_caption = "Table 3: Impact of CPs near school by gender"
local tab_notes = "Notes: This table presents estimated coefficients from equation (1), modified by interacting the conflict variables with an indicator that equals 1 if the student is male. The same set of control variables and fixed effects are included as in the baseline equation. All regressions include school and academic year fixed effects. Standard errors in parentheses, clustered at the school locality level. * p<0.10 ** p<0.05 *** p<0.01."

esttab pass std_score std_math std_ara, noomitted keep(${keepvar}) coeflabels(${varlabels}) mtitles(${mtitles}) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(got_cp_0_10km "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes brackets $starlevel $tabwidth

if `make_csv_tables'==1 {
esttab pass std_score std_math std_ara using "$root/Output/Table3.csv", replace keep(${keepvar}) coeflabels(${varlabels}) mtitles(${mtitles}) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(got_cp_0_10km "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes gaps $starlevel
}

}

/*** ---------------------------------------------------------------------------
	 TABLE 4: IMPACT OF ENCOUNTERING A CP ON THE ROAD TO SCHOOL 
--------------------------------------------------------------------------- ***/
if `tab_4' == 1 {

use "$cleandata/main_data.dta", clear

global y_vars = "pass std_score std_math std_ara" 
global stu_cntl = "female i.religion i.branch i.birth_year i.year" // student controls
global school_cntl = "num_classrooms total_students total_teachers" // school controls 

bys origin: g uniq_orig = 1 if _n==1 
bys destination: g uniq_des = 1 if _n==1 

foreach y in $y_vars {

if "`y'" == "pass" local y_untransformed = "pass_exam"
if "`y'" == "std_score" local y_untransformed = "average"
if "`y'" == "std_math" local y_untransformed = "math"
if "`y'" == "std_ara" local y_untransformed = "ara"

// NO SCHOOL FIXED EFFECTS 
qui areg `y' i.cross_cp sch_fatalities stu_fatalities sch_pop10km stu_pop10km ${stu_cntl} ${school_cntl} i.dist_bins i.destination, absorb(origin) vce(cluster od)

qui qui su `y_untransformed'
qui estadd scalar y_mu=r(mean)
qui estadd scalar y_sd=r(sd)

qui estadd local stu_cntl "Y" 
qui estadd local sch_cntl "Y" 
qui estadd local orig_FE "Y" 
qui estadd local des_FE "Y" 
qui estadd local year_FE "Y"
qui estadd local school_FE "" 
qui count if uniq_orig == 1 
qui estadd scalar num_origin=r(N)
qui count if uniq_des == 1
qui estadd scalar num_des=r(N)

est sto `y'1

// W/ SCHOOL FIXED EFFECTS 
qui areg `y' i.cross_cp sch_fatalities stu_fatalities sch_pop10km stu_pop10km ${stu_cntl} ${school_cntl} i.dist_bins i.destination i.school_id, absorb(origin) vce(cluster od)

qui qui su `y_untransformed'
qui estadd scalar y_mu=r(mean)
qui estadd scalar y_sd=r(sd)

qui estadd local stu_cntl "Y" 
qui estadd local sch_cntl "Y" 
qui estadd local orig_FE "Y" 
qui estadd local des_FE "Y" 
qui estadd local year_FE "Y"
qui estadd local school_FE "Y" 
qui count if uniq_orig == 1 
qui estadd scalar num_origin=r(N)
qui count if uniq_des == 1
qui estadd scalar num_des=r(N)

est sto `y'2

}

global mtitles = `" "Pass" "Overall score" "Maths" "Arabic" "'

global keepvar = "1.cross_cp sch_fatalities stu_fatalities"
global varlabels = `" 1.cross_cp "Encounters checkpoint" sch_fatalities "Fatalities (School locality)" stu_fatalities "Fatalities (Home locality)" "'  

global stats = `" y_mu y_sd stu_cntl sch_cntl orig_FE des_FE year_FE school_FE num_origin num_des N ,fmt(%6.2f %6.2f %~#s %~#s %~#s %~#s %~#s %~#s %18.0f %18.0f %18.0f) labels("Dep var mean" "Dep var SD" "Student controls" "School controls" "Home locality FE" "School locality FE" "Year FE" "School FE" "Num home localities" "Num school localities" "Observations") "'

local tab_notes = "Notes: This table presents estimated coefficients from equation (2) where the obstacle of interest are checkpoints. Scores expressed in standard deviations normalized by study stream and year. All regressions include the following controls: population size of Israeli settlements within 10 km of school and home locality (in 100s), student controls (gender, religion, year of birth, study branch) and school controls (number of classrooms, number of students, number of teachers). All regressions include home locality, school locality, and academic year fixed effects. Standard errors in parentheses, clustered at the home-school locality pair level.* p<0.10 ** p<0.05 *** p<0.01."

esttab pass1 std_score1 std_math1 std_ara1, noomitted keep(${keepvar}) coeflabels(${varlabels}) mtitle($mtitles) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(Table 4: Impact of encountering CP on road to school (Odd columns, no school FE)) refcat(1.cross_cp "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes gaps $starlevel $tabwidth

esttab pass2 std_score2 std_math2 std_ara2, noomitted keep(${keepvar}) coeflabels(${varlabels}) mtitle($mtitles) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(Table 4: Impact of encountering CP on road to school (Even columns, w/ school FE)) refcat(1.cross_cp "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes gaps $starlevel $tabwidth

if `make_csv_tables' == 1 {
esttab pass1 pass2 std_score1 std_score2 std_math1 std_math2 std_ara1 std_ara2 using "$root/Output/Table4.csv" , replace noomitted keep(${keepvar}) coeflabels(${varlabels}) nomtitle mgroups("Pass" "Overall score" "Maths" "Arabic" , pattern(1 0 1 0 1 0 1 0)) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(1.cross_cp "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes gaps $starlevel
}

}

/*** ---------------------------------------------------------------------------
	 TABLE 5: 
--------------------------------------------------------------------------- ***/
if `tab_5' == 1 {

use "$cleandata/main_data.dta", clear

gen science = branch == 2 if !mi(branch)
gen art = branch == 1 if !mi(branch)

collapse (sum) science art (mean) scilab computer_lab total_* sch_num_cp_* sch_fatalities sch_pop10km destination, by(school_id year)

qui merge 1:1 school_id year using "$cleandata/teachers"
drop if _merge == 2
drop _merge 

*** Regressions: teachers 
gen got_cp_0_10km = (sch_num_cp_0_5km>=1 | sch_num_cp_5_10km>=1)


foreach y in traverse_teacher experience {

qui reghdfe `y' got_cp_0_10km sch_fatalities sch_pop10km [aw=total_teacher], absorb(school_id year) vce(cluster destination)

qui su `y'
qui estadd scalar y_mu=r(mean)
qui estadd scalar y_sd=r(sd)

qui estadd local sch_FE "Y" 
qui estadd local year_FE "Y" 
qui count if !mi(`y') 
qui estadd scalar obs = r(N)

est sto `y'
}

*** Regressions: environment 
foreach y in scilab computer_lab {

qui reghdfe `y' got_cp_0_10km sch_fatalities sch_pop10km science, absorb(school_id year) vce(cluster destination)

qui su `y'
qui estadd scalar y_mu=r(mean)
qui estadd scalar y_sd=r(sd)

qui estadd local sch_FE "Y" 
qui estadd local year_FE "Y" 
qui count if !mi(`y')
qui estadd scalar obs = r(N)

est sto `y'
}

global mtitles = `" "% Teacher traversing" "Experience" "Science lab" "Computer lab" "'

global keepvar = "got_cp_0_10km sch_fatalities"
global varlabels = `" got_cp_0_10km ">= 1 CP within 10 km" sch_fatalities "Fatalities (100s)" "'  

global stats = `" y_mu y_sd sch_FE year_FE df_a_nested N_clust obs ,fmt(%6.2f %6.2f %~#s %~#s %18.0f %18.0f %18.0f) labels("Dep var mean" "Dep var SD" "School FE" "Year FE" "Num schools" "Num school localities" "Observations") "'

local tab_caption = "Table 5: Impact of checkpoints (CPs) near school on school learning environment"
local tab_notes = "Notes: All regressions include the following controls: population size of Israeli settlements within 10 km of school locality (in 100s), school fixed effects, and academic year fixed effects. Regressions in columns (1) and (2) are weighted by the number of teachers. Columns (3) and (4) add additional controls for the number of students in the Science stream. Standard errors in parentheses, clustered at the school locality level.* p<0.10 ** p<0.05 *** p<0.01."

esttab traverse_teacher experience scilab computer_lab, noomitted keep(${keepvar}) coeflabels(${varlabels}) mtitles(${mtitles}) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(got_cp_0_10km "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes brackets $starlevel $tabwidth


if `make_csv_tables' == 1 {

esttab traverse_teacher experience scilab computer_lab using "$root/Output/Table5.csv" , replace keep(${keepvar}) coeflabels(${varlabels}) mtitles(${mtitles}) b(%6.3f) se(%6.3f) stats(${stats}) collabels(none) title(`tab_caption') refcat(got_cp_0_10km "A. Mobility restrictions" sch_fatalities "B. Other conflict variables", nolabel) addnotes("`tab_notes'") nonotes $starlevel 
}

}

/*** -------------------------------------------------------------------------------
	 FIGURE 1: This figure is produced by ARIJ
------------------------------------------------------------------------------- ****/

if `fig_1' == 1 {

di as err "This figure is produced by ARIJ."
}

/*** -------------------------------------------------------------------------------
	 FIGURE 2: EVOLUTION OF CONFLICT 
------------------------------------------------------------------------------- ****/

if `fig_2' == 1 {

use "$rawdata/cp_dis.dta", clear
bys facilityid year: keep if _n==1
g unit = 1
bys year: egen num_cp = total(unit)
collapse num_cp, by(year)
tempfile cp
save `cp', replace 

use "$rawdata/em_dis.dta", clear
bys facilityid year: keep if _n==1
g unit = 1
bys year: egen num_em = total(unit)
collapse num_em, by(year)
tempfile em
save `em', replace 

use "$rawdata/other_dis.dta", clear
bys facilityid year: keep if _n==1
g unit = 1
bys year: egen num_oth = total(unit)
collapse num_oth, by(year)
tempfile oth
save `oth', replace 

use "$rawdata/rb_dis.dta", clear
bys facilityid year: keep if _n==1
g unit = 1
bys year: egen num_rb = total(unit)
collapse num_rb, by(year)
tempfile rb
save `rb', replace 

use "$rawdata/Pal_FINAL_AUG13", clear
drop if (year == 0 | mi(year))
foreach v in minor_fat fat_byciv fat_bysecurity fat_bypalest {
replace `v' = 0 if mi(`v')
}
gen fatal = minor_fat + fat_byciv + fat_bysecurity + fat_bypalest
bys year: egen fatalities = total(fatal)
collapse fatalities, by(year)
tempfile fatal
save `fatal', replace 

use `cp', clear
qui merge 1:1 year using `em'
drop _merge
qui merge 1:1 year using `oth'
drop _merge
qui merge 1:1 year using `rb'
drop _merge
qui merge 1:1 year using `fatal'

foreach var of varlist _all {
	replace `var' = 0 if `var'==.
}
g num_other = num_em + num_oth + num_rb 
drop num_em num_oth num_rb

keep if year>=2000 & year<=2006

cap drop year?
gen year1 = year-0.2
gen year2 = year
gen year3 = year+0.2

twoway bar num_cp year1, barw(0.2) yaxis(1) fcolor(none) lcolor(black) || ///
	   bar num_other year2, barw(0.2) yaxis(2) fcolor(gs10) lcolor(gs10) || ///
	   bar fatalities year3, barw(0.2) yaxis(2) fcolor(black) lcolor(black) ///
	   title("Evolution of conflict over time in the West Bank", size(medlarge)) ///
	   ytitle("Num. of checkpoints", axis(1)) ///
	   ytitle("Num. of fatalities/other barriers", axis(2)) /// 
	   yscale(range(0 100) axis(2)) ///
	   ylabel(0(50)100, axis(1)) ///
	   xtitle("Year") ///
	   xlabel(2000(2)2006 , labsize(small)) ///
	   legend(order(1 "Checkpoints" 2 "Other barriers" 3 "Fatalities") ///
	   col(3) size(small) symxsize(*0.25)) ///
	   graphregion(color(white)) ///
	   plotregion(margin(b=0))

graph export "$root/Output/Figure2.tif", as(tif) width(2000) replace

}

/*** -------------------------------------------------------------------------------
	 FIGURE 3: 
------------------------------------------------------------------------------- ****/

if `fig_3' == 1 {

use "$cleandata/main_data.dta", clear

gen got_cp_0_10km = (sch_num_cp_0_5km>=1 | sch_num_cp_5_10km>=1)

global y_vars = "pass std_score std_math std_ara" 
global stu_cntl = "female i.religion i.branch i.birth_year i.year"
global school_cntl = "num_classrooms total_students total_teachers"
global sch_oth_conflict = "sch_fatalities"
global sch_loc_cntl = "sch_pop10km"
global FE = "school_id"
global cluster_var = "destination"

cap drop cp*
gen cp1_0_10km = (sch_num_cp_0_5km + sch_num_cp_5_10km==1)
gen cp2_0_10km = (sch_num_cp_0_5km + sch_num_cp_5_10km==2|sch_num_cp_0_5km + sch_num_cp_5_10km==3|sch_num_cp_0_5km + sch_num_cp_5_10km==4)
gen cp3_0_10km = (sch_num_cp_0_5km + sch_num_cp_5_10km>=5)

foreach y in $y_vars {

if "`y'" == "pass" local y_untransformed = "pass_exam"
if "`y'" == "above_65" local y_untransformed = "above_65"
if "`y'" == "std_score" local y_untransformed = "average"
if "`y'" == "std_math" local y_untransformed = "math"
if "`y'" == "std_ara" local y_untransformed = "ara"


qui areg `y' cp1_0_10km cp2_0_10km cp3_0_10km ${sch_oth_conflict} ${stu_cntl} ${school_cntl} ${sch_loc_cntl}, absorb(${FE}) vce(cluster ${cluster_var}) 

est store `y'


}

coefplot (pass, offset(-0.3) msymbol(C)  mlabposition(1) mcolor(black)) ///
		 (std_score, offset(-0.1) msymbol(D)  mlabposition(1) mcolor(black)) ///
		 (std_math, offset(0.1) msymbol(S)  mlabposition(1) mcolor(black)) ///
		 (std_ara, offset(0.3) msymbol(T)  mlabposition(1) mcolor(black)), ///
		 keep(cp1_0_10km cp2_0_10km cp3_0_10km) ///
		 coeflabels(cp1_0_10km=`" "1 Checkpoint" "(1st/2nd quartile)" "'  cp2_0_10km=`" "2-4 Checkpoints" "(3rd quartile)" "' cp3_0_10km=`" ">=5 Checkpoints" "(4th quartile)" "', noticks labsize(small) labcolor(black)) ///
		 vertical yline(0, lcolor(red) lpattern(dash)) ///
		 mlabel format(%9.3f) mlabcolor(black) mlabsize(small) mlabgap(0.1) mfcolor(black) ///
		 ciopt(recast(rcap) lcolor(black)) ///
		 ytitle("Coefficient estimate (95% CI)", margin(small)) ///
		 title("Effect of additional checkpoints on academic performance", size(medium) margin(medium)) ///
		 yscale(range(-0.2 0.05)) ylabel(-0.2(0.1)0.0,format(%2.1f)) ///
		 legend(order(2 "Pass" 4 "Overall score" 6 "Math" 8 "Arabic") ring(0) row(1) bplacement(s) symxsize(*0.5) size(small) region(lwidth(none))) ///
		 graphregion(color(white)) 

graph export "$root/Output/Figure3.tif", as(tif) width(2000) replace

}

/*** -------------------------------------------------------------------------------
------------------------------------------------------------------------------- ****/

cap log close 
